#!/usr/bin/env bash
rm -f /home/services/logs/start_script.done

env=$1

cd /home/services/clotho-cappy-internal-server/bin

if [ -z '${env}' ];
      then ${env}='dev'
fi

if [ "$JACOCO_PORT" != "" ]; then
     export JAVA_OPT=" -javaagent:/opt/jacoco/lib/jacocoagent.jar=output=tcpserver,address=0.0.0.0,port=16031"
fi

if [ ${env} == "prod" ] ; then
   JAVA_OPT="${JAVA_OPT} -Xms3g -Xmx3g
                         -Dskywalking.plugin.kafka.bootstrap_servers=msk-skywalking1.prod-spatio-inc.com:9092,msk-skywalking2.prod-spatio-inc.com:9092,msk-skywalking3.prod-spatio-inc.com:9092"
elif [ ${env} == "pre" ] ; then
   JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m
                         -Dskywalking.plugin.kafka.bootstrap_servers=msk-skywalking1.prod-spatio-inc.com:9092,msk-skywalking2.prod-spatio-inc.com:9092,msk-skywalking3.prod-spatio-inc.com:9092"
elif [ ${env} == "pt" ] ; then
  JAVA_OPT="${JAVA_OPT} -Xms4g -Xmx4g
                         -Dskywalking.plugin.kafka.bootstrap_servers=b-1.msk-public1.pt-spatio-inc.com:9092,msk-public2.pt-spatio-inc.com:9092,msk-public3.pt-spatio-inc.com:9092"
else
   JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m
                         -Dskywalking.plugin.kafka.bootstrap_servers=b-2.mskclothopubjptes.acwqt5.c2.kafka.ap-northeast-1.amazonaws.com:9092,b-1.mskclothopubjptes.acwqt5.c2.kafka.ap-northeast-1.amazonaws.com:9092,b-3.mskclothopubjptes.acwqt5.c2.kafka.ap-northeast-1.amazonaws.com:9092"
fi

START_LOG='/home/services/logs/info.log'

service_name="clotho-activity-internal-server"
if [ -n "${K8S_SERVICE}" ]; then
  service_name="${K8S_SERVICE}"
fi

#JAVA_OPT="${JAVA_OPT}  -XX:SurvivorRatio=8
#                -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75
#                -XX:MaxTenuringThreshold=15 -XX:PretenureSizeThreshold=2M
#                -XX:-OmitStackTraceInFastThrow
#                -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps
#                -Dfastjson.parser.safeMode=true
#                -Dfile.encoding=UTF8
#                -Dspring.profiles.active=${env}
#                -javaagent:/home/services/skywalking-8.12.0/skywalking-agent/skywalking-agent.jar
#                -Dskywalking.agent.service_name=clotho-${env}::${service_name}
#                -Dskywalking.plugin.springmvc.collect_http_params=true
#                -Dskywalking.plugin.httpclient.collect_http_params=true
#                -Dskywalking.plugin.jdbc.trace_sql_parameters=true
#                -Dskywalking.plugin.lettuce.trace_redis_parameters=true
#                -Dskywalking.plugin.redisson.trace_redis_parameters=true
#                -jar *.jar"

# use g1
JAVA_OPT="${JAVA_OPT}  -XX:+UseG1GC
                -XX:+UseCompressedClassPointers
                -XX:+UseCompressedOops
                -XX:MaxMetaspaceSize=256m
                -XX:MetaspaceSize=256m
                -XX:MaxGCPauseMillis=200
                -Dfastjson.parser.safeMode=true
                -Dfile.encoding=UTF8
                -Dspring.profiles.active=${env}
                -javaagent:/home/services/skywalking-8.12.0/skywalking-agent/skywalking-agent.jar
                -Dskywalking.agent.service_name=clotho-${env}::${service_name}
                -Dskywalking.plugin.springmvc.collect_http_params=true
                -Dskywalking.plugin.httpclient.collect_http_params=true
                -Dskywalking.plugin.jdbc.trace_sql_parameters=true
                -Dskywalking.plugin.lettuce.trace_redis_parameters=true
                -Dskywalking.plugin.redisson.trace_redis_parameters=true
                -jar *.jar"

PROG=$JAVA_HOME/bin/java
nohup $PROG ${JAVA_OPT} > ${START_LOG} 2>&1 &
